﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

using Microsoft.Office.Core;

namespace ConvertOffice
{
    public class Word2Html
    {
        public void Excute(string srcPath, string targetDir)
        {
            //在创建Word对象的时候一定要逐级创建，不要遗漏，这样做是为了后面的释放资源
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Documents docs = null;
            Microsoft.Office.Interop.Word.Document doc = null;

            try
            {
                app = new Microsoft.Office.Interop.Word.Application();
                docs = app.Documents;

                bool readOnly = false;
                doc = docs.Open(srcPath,
                                    Type.Missing,
                                    readOnly,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing,
                                    Type.Missing);

                //如果文档有密码
                if (doc.HasPassword)
                    throw new Exception("文档有保护限制");

                //word另存为html后，html文件名
                string htmlFileName = targetDir + Path.DirectorySeparatorChar + "index.html";
                //word另存为html后，文件夹名
                string htmlFolder = targetDir + Path.DirectorySeparatorChar + "index.files";
                //存放另存为的html文件夹是否存在
                if (!Directory.Exists(targetDir))
                    Directory.CreateDirectory(targetDir);

                // 另存为 html
                doc.WebOptions.Encoding = MsoEncoding.msoEncodingUTF8;
                doc.SaveAs2(htmlFileName,
                            Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing);
            }
            finally
            {
                //释放资源的时候应该遵循原则 最后创建，最先释放
                if (doc != null)
                {
                    //第一个参数必须要
                    doc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges, Type.Missing, Type.Missing);
                    Marshal.ReleaseComObject(doc);
                    doc = null;
                }
                if (app != null)
                {
                    app.Quit(Type.Missing, Type.Missing, Type.Missing);
                    Marshal.ReleaseComObject(app);
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}
